package com.jic.veteran.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.jic.veteran.component.VeteranSecurityContext;
import com.jic.veteran.mapper.ProjectBasicMapper;
import com.jic.veteran.mapper.ProjectThanksMapper;
import com.jic.veteran.model.ProjectBasic;
import com.jic.veteran.model.ProjectBasicExample;
import com.jic.veteran.model.ProjectThanks;
import com.jic.veteran.model.ProjectThanksExample;
import com.jic.veteran.service.ProjectThanksService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProjectThanksServiceImpl implements ProjectThanksService {
    private static final Logger LOGGER = LoggerFactory.getLogger(ProjectThanksServiceImpl.class);

    @Autowired
    private ProjectThanksMapper projectThanksMapper;

    @Autowired
    private ProjectBasicMapper projectBasicMapper;

    @Override
    public List<ProjectThanks> list(String projectId, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        ProjectThanksExample example = new ProjectThanksExample();
        if(StrUtil.isNotEmpty(projectId)) {
            example.createCriteria().andProjectIdLike("%" +projectId +"%" );
        }
        return projectThanksMapper.selectByExample(example);
    }

    @Override
    public int create(ProjectThanks projectThanks) {
        ProjectBasicExample projectBasicExample = new ProjectBasicExample();
        projectBasicExample.createCriteria().andProjectIdEqualTo(projectThanks.getProjectId());
        List<ProjectBasic> projectBasicList = projectBasicMapper.selectByExample(projectBasicExample);
        if(!projectBasicList.isEmpty()) {
            ProjectBasic projectBasic = projectBasicList.get(0);
            if(null != projectBasic) {
                projectBasic.setProjectId(projectBasic.getProjectId());
                projectThanks.setProjectName(projectBasic.getProjectName());
                projectThanks.setProjectThanks(projectThanks.getProjectThanks());
                projectThanks.setProjectThanksStatus(projectThanks.getProjectThanksStatus());
                projectThanks.setCreateBy(VeteranSecurityContext.getUserName());
                projectThanks.setCreateTime(DateUtil.date());
                projectThanks.setModifyBy(VeteranSecurityContext.getUserName());
                projectThanks.setModifyTime(DateUtil.date());
                return projectThanksMapper.insert(projectThanks);
            }
        }
        return 0;
    }

    @Override
    public int update(Long id,ProjectThanks projectThanks) {
        projectThanks.setId(id);
        projectThanks.setModifyBy(VeteranSecurityContext.getUserName());
        projectThanks.setModifyTime(DateUtil.date());
        return projectThanksMapper.updateByPrimaryKeySelective(projectThanks);
    }

    @Override
    public ProjectThanks getItem(Long id) {
        return projectThanksMapper.selectByPrimaryKey(id);
    }

    @Override
    public int delete(Long id) {
        return projectThanksMapper.deleteByPrimaryKey(id);
    }
}
